package fatboy.addons;
import fatboy.communication.ObjectID;
import fatboy.communication.Location;
import fatboy.communication.RemoteLocation;
import fatboy.communication.Command;
import fatboy.communication.ResidentDatabase;
import java.io.IOException;
import java.io.Serializable;
public class ProxyFinalListener extends FinalListener implements Serializable{
	private ObjectID id;
	private static final long serialVersionUID = 2159800065695342256L;
	public ProxyFinalListener(ObjectID other){
		super();
		id = new ObjectID(other);
	}
	private static Object createCommand(ObjectID idIn, String meth, Class<Object>[] types,
		Object[] params, boolean returns, Location location){
		Object result = null;
		try {
			Command cmd = new Command(idIn,meth,types,params,
			ProxyFinalListener.class.getSuperclass());
			location.sendCommand(cmd);
			result = location.recieveReturn();
			location.close();
		} catch (IOException e) {
			e.printStackTrace();
			} catch (SecurityException e) {
			e.printStackTrace();
			}catch (ClassNotFoundException e) {
			e.printStackTrace();
			}
			return result;
	}
	public Object writeReplace(){
		return this;
	}
	public Object readResolve(){
		if(getLocation(this.id).isHere()){
			return ResidentDatabase.getObject(id);
		}
		return this;
	}
public Location getLocation(ObjectID id) {
	return new RemoteLocation((ObjectID)id);
	}

	public void inc(int param1){
		String meth = "inc";
		Class[] types ={
			int.class
			};
		Object[] params = {
			param1
			};
		createCommand(id,meth,types,params,false, getLocation(id));
	}
}

